<template>
  <div class="model">
    <div class="mask"></div>
    <div class="loginbox">
      <div class="close" @click="changeIsLoginShow(false)"></div>

      <section class="nav">
        <a
          href="#"
          :class="isShowLogin ? 'active' : ''"
          @click="isShowLogin = true"
          >手机号登录</a
        >
        |
        <a href="#" :class="isShowLogin ? '' : 'active'" @click="showQR"
          >微信扫码登录</a
        >
      </section>
      <div class="content">
        <!-- 手机号内容 -->
        <div class="phoneLogin" v-show="isShowLogin">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              class="ipt"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="145"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="imgs"
              :accuracy="accuracy"
            ></slide-verify>
          </div>
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机验证码"
              class="ipt"
              v-model="code"
            />
            <button class="btncode" @click="getCode" v-show="!isShowCount">
              获取验证码
            </button>
            <button class="btncode" v-show="isShowCount">
              {{ count + "s" }}
            </button>
          </div>
          <div class="row">
            <button class="btnlogin" @click="subFn">登录</button>
          </div>
        </div>
        <!-- 微信扫码登录内容 -->
        <div class="qrLogin" v-show="!isShowLogin" id="weixin">微信</div>
      </div>
    </div>
  </div>
</template>

<script>
import img1 from "../assets/imgs/img.jpg";
import img2 from "../assets/imgs/img1.jpg";
import img3 from "../assets/imgs/img2.jpg";
import img4 from "../assets/imgs/img3.jpg";
import { sendSMSAPI, PhoneRegin, BangDingAPI } from "@/request/api";
import { mapMutations, mapActions } from "vuex";
import { validatePhoneNumber } from "../utils/index";
export default {
  name: "LaoliuLogin",

  data() {
    return {
      code: "",
      isShowLogin: true,
      msg: "向右滑动",
      imgs: [img1, img2, img3, img4],
      accuracy: 10000,
      phoneNumber: "",
      count: 60,
      isShowCount: false,
    };
  },

  mounted() {},

  methods: {
    ...mapMutations({
      changeIsLoginShow: "showModal/changeIsLoginShow",
      loginStatus: "logiinStatus/loginStatus",
      // changeIsShowToast:"showToast/changeIsShowToast",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    daojishi() {
      this.isShowCount = true;
      let timer = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          this.count = 60;
          clearInterval(timer);
          this.isShowCount = false;
        }
      }, 1000);
    },
    toVerify() {
      if (!validatePhoneNumber(this.phoneNumber)) {
        // alert('请输入正确的手机号码！')
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "请输入正确的手机号",
          type: "warning",
        });
        // setTimeout(() => this.changeIsShowToast({
        //   isshow:false
        // }), 1000);
        return;
      }
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        // alert("请滑动滑块验证码！")
        return;
      }
      return true;
    },
    //登录
    async subFn() {
      if (!this.toVerify()) return;
      if (this.code.trim() == "") {
        // alert('请输入正确的验证码')
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "请输入正确的验证码",
          type: "warning",
        });
        return;
      }
      // 发送登录请求
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        res = await BangDingAPI({
          verifyCode: this.code,
          phone: this.phoneNumber,
          uuid,
        });
      } else {
        res = await PhoneRegin({
          verifyCode: this.code,
          phone: this.phoneNumber,
        });
      }

      if (!res) return;
      // alert('登录失败')
      this.asyncChangeIsShowToast({
        isshow: true,
        msg: "登录成功",
        type: "success",
      });
      localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
      this.changeIsLoginShow(false);
      this.loginStatus(true);
      // 清除code
      if (uuid) {
        console.log(123)
         this.$router.push(this.$route.path);
      }

      // 删除uuid
      localStorage.removeItem("uuid");
    },
    // 获取验证码
    async getCode() {
      let flag = this.toVerify();
      if (!flag) return;
      let res = await sendSMSAPI({
        phone: this.phoneNumber,
      });

      if (!res) return;
      // alert('发送成功')
      this.asyncChangeIsShowToast({
        isshow: true,
        msg: "发送成功",
        type: "success",
      });
      this.daojishi();
    },
    showQR() {
      this.isShowLogin = false;
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,Lyogd3hsb2dpbi5jc3MgKi8NCi5pbXBvd2VyQm94IC50aXRsZSwgLmltcG93ZXJCb3ggLmluZm97DQogIGRpc3BsYXk6IG5vbmU7DQp9DQoNCi5pbXBvd2VyQm94IC5xcmNvZGV7DQogIG1hcmdpbi10b3A6IDIwcHg7DQp9",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.model {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .mask {
    width: 100%;
    height: 100%;

    background-color: rgba(0, 0, 0, 0.5);
  }
  .loginbox {
    text-align: center;
    font-size: 28px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    height: 423px;
    width: 555px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      position: absolute;
      right: 70px;
      top: 15px;
      width: 22px;
      height: 22px;
      cursor: pointer;
      background: url("../assets/img/close.png");
    }
    .nav {
      margin-top: 40px;
      font-size: 18px;
      color: #ccc;
      // 清楚a标签默认样式
      a {
        text-decoration: none;
        color: darkgrey;
        &.active {
          color: #0a328e;
        }
      }
    }
  }
  .content {
    width: 335px;
    height: 300px;
    margin: auto;
    .phoneLogin {
      margin-top: 20px;

      .row {
        display: flex;
        // width: 335px;
        margin-top: 20px;
        .ipt {
          height: 50px;
          outline: none;
          flex: 1;
          text-indent: 1em;
        }
        .btncode {
          width: 85px;

          border: none;
          background-color: #0a328e;
          color: #fff;
          margin-left: 10px;
        }
        .btnlogin {
          width: 100%;
          height: 50px;
          border: none;
          background-color: #0a328e;
          color: #fff;
        }
      }
    }
  }
}
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>